<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>短信明细</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/font-awesome.min93e3.css"/>
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/plugins/datapicker/bootstrap-datetimepicker.min.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/style.min.css"/>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.15/css/jquery.dataTables.min.css" rel="stylesheet">
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<style type="text/css">
body{font-size: 14px;padding: 10px;}
.dataTables_info {font-size: 12px}
.dataTables_paginate {font-size: 12px;}
</style>
</head>
<body class="gray-bg">
<div class="container-fluid">
	<div class="row" style="background-color: white;padding: 16px 0;line-height: 34px;">
		<div class="col-sm-2">
			发送短信：<span style="color: #1ab394;font-size: 16px;"><span id="sendCount"></span>条</span>
		</div>
		<div class="col-sm-2">
			剩余短信：<span style="color: red;font-size: 16px;"><span id="usableCount"></span>条</span>
		</div>
		<div class="col-sm-2">
			<select id="msg_type" class="form-control">
				<option value="">全部</option>
				<option value="1">系统短信</option>
				<option value="2">群发短信</option>
				<option value="3">短信充值</option>
				<option value="4">通知短信</option>
			</select>
		</div>
		<div class="col-sm-3">
			<div class="input-daterange input-group">
	            <input type="text" class="input-sm form-control" name="datetimepicker_start" readonly style="background-color: white;">
	            <span class="input-group-addon" style="background-color: #eeeeee;">到</span>
	            <input type="text" class="input-sm form-control" name="datetimepicker_end" readonly style="background-color: white;">
	        </div>
		</div>
		<div class="col-sm-3 text-right">
		    <shiro:hasPermission name="set_smsList:export">
			<a href="javascript:void(0)" onclick="func_smsLogDownload(this)"><button class="btn btn-primary" type="button">导出</button></a>
			</shiro:hasPermission>
			<button class="btn btn-primary" type="button" style="display: none;">短信充值</button>
		</div>
	</div>
	<div style="height: 20px;"></div>
	<div class="row">
		<table id="myTable" class="display">
			<thead>
				<tr>
					<th>短信类型</th>
					<th>短信内容</th>
	                <th>时间</th>
	                <th>短信数量</th>
	                <th>短信明细</th>
				</tr>
			</thead>
		</table>
	</div>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="<%=path%>/hplus/js/plugins/datapicker/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript" src="<%=path%>/hplus/js/plugins/datapicker/bootstrap-datetimepicker.zh-CN.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="<%=path%>/js/dateUtils.js"></script>
<script type="text/javascript">
var path = "<%=path%>";
$.fn.dataTable.ext.errMode = 'none';
$(function(){
	$("input[name='datetimepicker_start']").datetimepicker({language:"zh-CN",format: 'yyyy-mm-dd',autoclose: true,minView:2})
	.on("click",function(){
        $("input[name='datetimepicker_start']").datetimepicker("setEndDate",$("input[name='datetimepicker_end']").val())
    }).on('changeDate', function(ev){
    	$(".btn-group>button:eq(0)").removeClass("btn-white").addClass("btn-primary").siblings().removeClass("btn-primary").addClass("btn-white");
    	func_dataTableReload();
    });
	$("input[name='datetimepicker_end']").datetimepicker({language:"zh-CN",format: 'yyyy-mm-dd',autoclose: true,minView:2}).on("click",function(){
        $("input[name='datetimepicker_end']").datetimepicker("setStartDate",$("input[name='datetimepicker_start']").val())
    }).on('changeDate', function(ev){
    	$(".btn-group>button:eq(0)").removeClass("btn-white").addClass("btn-primary").siblings().removeClass("btn-primary").addClass("btn-white");
    	func_dataTableReload();
    });
	
	$("#msg_type").change(function(){
		func_dataTableReload();
	});
	
	init();
});

function init(){
	func_smsTotal();
	
	myDataTable = dataTable();
}

//短信统计
function func_smsTotal(){
	$.post(path+"/smsStatistics/ajaxSmsTotal",function(data){
		$("#sendCount").text(data.sendCount);
		$("#usableCount").text(data.usableCount);
	});
}

function func_dataTableReload(resetPaging) {
	if(resetPaging != undefined){
	    myDataTable.ajax.reload(null,resetPaging);
	}else{
		myDataTable.ajax.reload();
	}
}


function func_AjaxUrl() {
    return path + "/smsStatistics/ajaxSmsPage";
}

function func_columns() {
    var jsonArr = [
    	{"data": "msgType"},
        {"data": "msgTittle"},
        {"data": "createTime"},
        {"data": "sumMsgCounts"},
        {"data": "msgType"},
    ];
    return jsonArr;
}

function func_ajaxData() {
    var jsonObject = new Object();
    var msg_type = $("#msg_type").val();
    if(msg_type != ""){
    	jsonObject.msg_type = msg_type;
    }
    var startTime = $("input[name='datetimepicker_start']").val();
    var endTime = $("input[name='datetimepicker_end']").val();
    if(startTime != ''){
    	jsonObject.startTime = startTime;
    }
    if(endTime != ''){
    	jsonObject.endTime = endTime;
    }
    return jsonObject;
}

function dataTable() {
    return $('#myTable').DataTable({
        processing: true,
        ordering: false,
        searching: false,
        info: true,
        autoWidth: false,
        lengthChange: false,
        language: {
            emptyTable: '没有数据表',
            info: '从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录',
            infoEmpty: '没有数据',
            lengthMenu: '每页显示 _MENU_ 条记录',
            loadingRecords: '正在加载数据....',
            processing: '正在处理数据....',
            zeroRecords: '没有找到匹配的记录',
            paginate: {
                first: '首页',
                previous: '前页',
                next: '后页',
                last: '尾页'
            }
        },
        order: [[1, 'desc']],
        pageLength: 10,
        pagingType: 'full_numbers',
        columnDefs: [{
            render: function (data, type, full) {
                var result = "";
                if(data == 1){
                	result = "系统短信";
                }else if(data == 2){
                	result = "群发短信";
                }else if(data == 3){
                	result = "短信充值";
                }else if(data == 4){
                	result = "通知短信";
                }
                return result;
            },
            targets: 0
		},{
            render: function (data, type, full) {
                
                return formatDateTimeYMDHMS(data);;
            },
            targets: 2
		},{
            render: function (data, type, full) {
                if(data==0){
                	return '<td style="color:red"><font color="red">没有余额未发出</td>';
                }else{
                	return data;
                }
                
            },
            targets: 3
		},{
            render: function (data, type, full) {
            	 var result = "";
                	var batch_num = full.batchNum;
                 if(data==2 || (data==4&&(batch_num.substr(batch_num.length-1)=="A"||batch_num.substr(batch_num.length-1)=="M"))){
                 	var msg_type = "";
                 	if(full.msgType == 1){
                 		msg_type = "系统短信";
                     }else if(full.msgType == 2){
                     	msg_type = "群发短信";
                     }else if(full.msgType == 3){
                     	msg_type = "短信充值";
                     }else if(full.msgType == 4){
                     	msg_type = "通知短信";
                     }
                 	result = '<shiro:hasPermission name="set_smsList:download">';
                 	result += "<a href='<%=path%>/smsStatistics/smsDetailDownload?batchNum="+batch_num+"&msgType="+encodeURI(encodeURI(msg_type))+"&msgTitle="+encodeURI(encodeURI(full.msgTittle))+"'  style='color:#1ab394'>短信明细</a>";
                 	result +='</shiro:hasPermission>';
                 }
                 return result;
            },
            targets: 4
		}],
        columns: func_columns(),
        serverSide: true,
        ajax: {
            contentType: 'application/json',
            type: 'POST',
            url: func_AjaxUrl(),
            data: function (d) {
                d.extra_search = func_ajaxData();
                return JSON.stringify(d);
            }
        }
	});
}

function func_smsLogDownload(that){
	var href = path+"/smsStatistics/smsLogDownload?r="+Math.random();
	if($("input[name='datetimepicker_start']").val() != ""){
		href += "&startTimeStr="+$("input[name='datetimepicker_start']").val();
	}
	if($("input[name='datetimepicker_end']").val() != ""){
		href += "&endTimeStr="+$("input[name='datetimepicker_end']").val();
	}
	$(that).attr("href",href);
}
</script>
</html>